#include <assert.h>
#include <string.h>
#include <stdio.h>
#include "drv_common_def.h"
#include "dev_common.h"
#include "dev_debug_serial_port.h"
#include "dev_led.h"
#include "dev_swi.h"
#include "drv_ed_utility.h"

ABS_CONSTRUCTOR(150)
void Common_BSP_Init(void)
{
    ED4E_EventProcessors_Init();
    Dev_SWI_Init();
    HAL_Init();
    SystemClock_Config();
    Dev_Delay_init();
    Dev_BoardLedInit();
    Dev_Debug_UartInit();
    HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

#define BIN_NUM_STR_LEN 50
char *Get_Binary(uint32_t data, char *buf, uint8_t bufLen)
{
    uint8_t bit = 0;
    uint8_t pos = 0;
    assert(bufLen >= BIN_NUM_STR_LEN);
    sprintf(buf, "| ");
    pos += 2;
    for (int i = 0; i < 32; i++) {
        bit = (data >> i) & 1;
        sprintf(buf + pos, "%d", bit);
        ++pos;
        if (i % 8 == 7) {
            sprintf(buf + pos, " | ");
            pos += 3;
        }
    }
    return buf;
}

#define BIN_NUM_STR_LEN 50
#define REGISTER_BIN_STR 100
void PrintRegister(char *varName, uint32_t var)
{
    char buff[REGISTER_BIN_STR] = {0};
    char binStr[BIN_NUM_STR_LEN] = {0};
    sprintf(buff, "%s = ", varName);
    uint8_t len = strlen(buff);
    while (len < 15) {
        sprintf(buff + len, " ");
        ++len;
    }
    sprintf(buff + len, "%s\n", Get_Binary(var, binStr, sizeof(binStr)));
    printf("%s", buff);
}